package com.ruoyi.gigi.sign.controller;

import com.alibaba.fastjson2.util.DateUtils;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gigi.course.domain.Course;
import com.ruoyi.gigi.course.service.ICourseService;
import com.ruoyi.gigi.enums.SignInTypeEnum;
import com.ruoyi.gigi.gigi_class.domain.GigiClass;
import com.ruoyi.gigi.gigi_class.service.IGigiClassService;
import com.ruoyi.gigi.sign.domain.SignIn;
import com.ruoyi.gigi.sign.service.ISignInService;
import com.ruoyi.gigi.user.domain.User;
import com.ruoyi.gigi.user.service.IUserService;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 考勤管理Controller
 *
 * @author gigi
 * @date 2024-01-30
 */
@RestController
@RequestMapping("/gigi.sign/sign")
public class SignInController extends BaseController {
    @Autowired
    private ISignInService signInService;
    @Autowired
    private IUserService userService;
    @Autowired
    private ICourseService courseService;
    @Autowired
    private IGigiClassService classService;

    /**
     * 查询考勤管理列表
     */
    @PreAuthorize("@ss.hasPermi('gigi.sign:sign:list')")
    @GetMapping("/list")
    public TableDataInfo list(SignIn signIn) {
        startPage();
        List<SignIn> list = signInService.selectSignInList(signIn);

        for (int i = 0; i < list.size(); i++) {
            SignIn e = list.get(i);
            Course course = courseService.selectCourseById(e.getCourseId());
            User user = userService.selectUserById(e.getUserId());

            if (Objects.isNull(course) || Objects.isNull(user)) {
                list.remove(i);

            } else {

                e.setUserName(user.getName());
                e.setUserNo(user.getNo());
                e.setCourseName(course.getName() + '(' + course.getNo() + ')');
                e.setTypeDesc(SignInTypeEnum.getEnumByCode(e.getType()).getDesc());
                e.setStartTimeStr(DateUtils.format(e.getStartTime(), "yyyy-MM-dd HH:mm:ss"));
                e.setEndTimeStr(DateUtils.format(e.getEndTime(), "yyyy-MM-dd HH:mm:ss"));

                List<String> classes = Arrays.stream(StringUtils.split(course.getClassId(), ",")).collect(Collectors.toList());
                List<String> classNames = classes.stream().map(c -> {
                    GigiClass gigiClass = classService.selectGigiClassById(Long.valueOf(c));
                    if (Objects.nonNull(gigiClass)) {
                        return gigiClass.getName();
                    } else {
                        return "未知";
                    }
                }).collect(Collectors.toList());
                e.setClassName(org.apache.commons.lang3.StringUtils.join(classNames, ","));
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出考勤管理列表
     */
    @PreAuthorize("@ss.hasPermi('gigi.sign:sign:export')")
    @Log(title = "考勤管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SignIn signIn) {
        List<SignIn> list = signInService.selectSignInList(signIn);
        ExcelUtil<SignIn> util = new ExcelUtil<SignIn>(SignIn.class);
        util.exportExcel(response, list, "考勤管理数据");
    }

    /**
     * 获取考勤管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('gigi.sign:sign:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(signInService.selectSignInById(id));
    }

    /**
     * 新增考勤管理
     */
    @PreAuthorize("@ss.hasPermi('gigi.sign:sign:add')")
    @Log(title = "考勤管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SignIn signIn) {
        return toAjax(signInService.insertSignIn(signIn));
    }

    /**
     * 修改考勤管理
     */
    @PreAuthorize("@ss.hasPermi('gigi.sign:sign:edit')")
    @Log(title = "考勤管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SignIn signIn) {
        return toAjax(signInService.updateSignIn(signIn));
    }

    /**
     * 删除考勤管理
     */
    @PreAuthorize("@ss.hasPermi('gigi.sign:sign:remove')")
    @Log(title = "考勤管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(signInService.deleteSignInByIds(ids));
    }
}
